import dayjs from "dayjs";
import axios from "axios";

String.reverse = (input) => {
	input = String(input);
	return input.split("").reverse().join("");
}


export function getMapDataList () {
	return [
		{name: '西藏', value: 0},
		{name: '上海', value: 0},
		{name: '广西', value: 0},
		{name: '广东', value: 0},
		{name: '云南', value: 0},
		{name: '香港', value: 0},
		{name: '澳门', value: 0},
		{name: '海南', value: 0},
		{name: '辽宁', value: 0},
		{name: '吉林', value: 0},
		{name: '宁夏', value: 0},
		{name: '青海', value: 0},
		{name: '内蒙古', value: 0},
		{name: '四川', value: 0},
		{name: '陕西', value: 0},
		{name: '重庆', value: 0},
		{name: '江苏', value: 0},
		{name: '贵州', value: 0},
		{name: '北京', value: 0},
		{name: '新疆', value: 0},
		{name: '浙江', value: 0},
		{name: '山东', value: 0},
		{name: '甘肃', value: 0},
		{name: '天津', value: 0},
		{name: '河南', value: 0},
		{name: '黑龙江', value: 0},
		{name: '河北', value: 0},
		{name: '湖南', value: 0},
		{name: '安徽', value: 0},
		{name: '湖北', value: 0},
		{name: '台湾', value: 0},
		{name: '山西', value: 0},
		{name: '福建', value: 0},
		{name: '江西', value: 0}
	];
}

export function isProductionMode () {
	return process.env.NODE_ENV === 'production'
}

export const isDevelopMode = () => {
	return process.env.NODE_ENV === 'development'
}

export const isTestMode = () => {
	return process.env.NODE_ENV === 'test'
}

export const formatTime = (date, formatter = "YYYY-MM-DD HH:mm:ss")=> {
	return dayjs(date).format(formatter);
}

export const readFile = async (filePath) => {

	const res = await axios.get(filePath);
	if (res.status = 200) {
		return res.data;
	} else {
		return null;
	}
}


const templateRegx = /<template([^>]*)>([\s\S\n]*?)<\/template>/i;
const jsRegx = /<script([^>]*)>([\s\S\n]*?)<\/script>/i;
const cssRegx = /<style([^>]*)>([\s\S\n]*?)<\/style>/i;

export const parseVue = (input) => {

	let template = input.match(templateRegx);
	let js = input.match(jsRegx);
	let css = input.match(cssRegx);

	if (template) {
		template = template[2];
	} else {
		template = "";
	}

	if (js) {
		js = js[2];

		js = js.replace("export default", "return");
		js = `function generate() {${js}; }`;

	} else {
		js = "";
	}

	if (css) {
		css = css[2];
	} else {
		css = "";
	}

	return {
		template, js, css
	};

}

export default {
	isDevelopMode,
	isProductionMode,
	isTestMode,
	formatTime,
	readFile,
	parseVue
}